/*
 * Copyright (C) 2015 yydcdut (yuyidong2015@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.yydcdut.sdlv;


import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

/**
 * Created by yuyidong on 2016/11/17.
 */
class SDMenuItemView extends BaseLayout {
    protected Image mImageView;
    protected Text mTextView;

    protected SDMenuItemView(Context context, MenuItem menuItem) {
        super(context, menuItem);
    }

    @Override
    protected void build() {
        StackLayout.LayoutConfig layoutParams = new StackLayout.LayoutConfig(mMenuItem.width,LayoutConfig.MATCH_PARENT);
        layoutParams.alignment = LayoutAlignment.CENTER;
        setLayoutConfig(layoutParams);
        addComponent(createBG());
        if (mMenuItem.text != null && mMenuItem.icon != 0) {
            mImageView = createImageView();
            mTextView = createTextView();
            DirectionalLayout linearLayout = new DirectionalLayout(getContext());
            linearLayout.setOrientation(VERTICAL);
            linearLayout.addComponent(mImageView);
            linearLayout.addComponent(mTextView);
            addComponent(linearLayout);
        } else if (mMenuItem.icon != 0) {
            mImageView = createImageView();
            addComponent(mImageView);
        } else if (mMenuItem.text != null) {
            mTextView = createTextView();
            addComponent(mTextView);
        } else {
            addComponent(createEmptyView());
        }
    }

    private Image createBG() {
        Image imageView = new Image(getContext());
        imageView.setBackground(mMenuItem.background);
        imageView.setWidth(mMenuItem.menuBtnWidth);
        imageView.setHeight(mMenuItem.menuBtnHeight);
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_PARENT,
                LayoutConfig.MATCH_PARENT);
        layoutConfig.setMargins(0,mMenuItem.marginTop,0,0);
        imageView.setLayoutConfig(layoutConfig);
        return imageView;
    }

    private Text createTextView() {
        Text textView = new Text(getContext());
        textView.setText(mMenuItem.text);
        textView.setTextSize(mMenuItem.textSize);
        textView.setTextColor(new Color(mMenuItem.textColor));
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_PARENT,
                LayoutConfig.MATCH_PARENT);
        layoutConfig.alignment = LayoutAlignment.VERTICAL_CENTER;
        textView.setLayoutConfig(layoutConfig);
        return textView;
    }

    private Component createEmptyView() {
        Component view = new Component(getContext());
        return view;
    }

    protected Image createImageView() {
        Image imageView = new Image(getContext());
        imageView.setPixelMap(mMenuItem.icon);
        return imageView;
    }

    @Override
    public Text getTextView() {
        return mTextView;
    }

    @Override
    public Image getImageView() {
        return mImageView;
    }
}


